
package com.saturday.uid.worker.service.impl;

import com.saturday.uid.worker.dao.WorkerNodeDao;
import com.saturday.uid.worker.service.WorkerIdAssigner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Represents an implementation of {@link WorkerIdAssigner},
 * the worker id will be discarded after assigned to the UidGenerator
 * 使用一次性的workerId时，建议增加workerBits位数
 */
@Service
public class DisposableWorkerIdAssigner implements WorkerIdAssigner {
    private static final Logger LOGGER = LoggerFactory.getLogger(DisposableWorkerIdAssigner.class);


    @Autowired
    private WorkerNodeDao workerNodeDao;

    /**
     * Assign worker id .<p>
     * If there is host name & port in the environment, we considered that the node runs in Docker container<br>
     * Otherwise, the node runs on an actual machine.
     * 
     * @return assigned worker id
     */
    @Override
    public long assignWorkerId() {
        return workerNodeDao.assignDisposableWorkerId();
    }

}
